using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;

namespace UglyAnimals
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class HighscoreScreen : GameScreen
    {
        /// <summary>
        /// Tracks a fading selection effect on the entry.
        /// </summary>
        /// <remarks>
        /// The entries transition out of the selection effect when they are deselected.
        /// </remarks>
        private float selectionFade;

        private ContentManager content;

        private StorageManager.HighscoreData highscores;

        /// <summary>
        /// Constructor.
        /// </summary>
        public HighscoreScreen()
        {
            StorageManager sm = StorageManager.GetInstance();
            highscores = sm.LoadHighScores();

            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }

        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");
        }

        /// <summary>
        /// Responds to user input, in this case only cancelling the screen.
        /// </summary>
        public override void HandleInput(InputState input)
        {
            // we cancel the current menu screen if the user presses the back button
            PlayerIndex player;
            if (input.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
            {
                OnCancel(player);
            }
        }

        /// <summary>
        /// Handler for when the user has cancelled the menu.
        /// </summary>
        protected virtual void OnCancel(PlayerIndex playerIndex)
        {
            ExitScreen();
        }


        /// <summary>
        /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
        /// </summary>
        protected void OnCancel(object sender, PlayerIndexEventArgs e)
        {
            OnCancel(e.PlayerIndex);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            // When the menu selection changes, entries gradually fade between
            // their selected and deselected appearance, rather than instantly
            // popping to the new state.
            float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;

            selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
        }

        public override void Draw(GameTime gameTime)
        {
            // Make the menu slide into place during transitions, using a
            // power curve to make things look more interesting (this makes
            // the movement slow down as it nears the end).
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            // start at Y = 175; each X value is generated per entry
            Vector2 position = new Vector2(200f, 175f);

            if (ScreenState == ScreenState.TransitionOn)
                position.X -= transitionOffset * 256;
            else
                position.X += transitionOffset * 512;

            Color color = Color.Black;

            // Pulsate the size of the selected menu entry.
            double time = gameTime.TotalGameTime.TotalSeconds;

            float pulsate = (float)Math.Sin(time * 6) + 1;

            float scale = 1 + pulsate * 0.05f * selectionFade;

            // Modify the alpha to fade text out during transitions.
            color *= TransitionAlpha;

            // Draw text, centered on the middle of each line.
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;

            spriteBatch.Begin();

            Vector2 origin = new Vector2(0, font.LineSpacing / 2);

            for (int i = 0; i < highscores.Count; i++)
            {
                spriteBatch.DrawString(font, (i + 1).ToString(), position, color, 0,
                       origin, scale, SpriteEffects.None, 0);

                position.X += 50;
                spriteBatch.DrawString(font, highscores.PlayerName[i], position, color, 0,
                       origin, scale, SpriteEffects.None, 0);

                position.X += 230;
                spriteBatch.DrawString(font, highscores.Score[i].ToString(), position, color, 0,
                       origin, scale, SpriteEffects.None, 0);

                position.X -= 280;
                // move down for the next entry the size of this entry plus our padding
                position.Y += font.LineSpacing + 10;
            }

            spriteBatch.End();
        }

    }
}
